package com.goodedeeds.domain.impl.db;

import com.goodedeeds.domain.IResourcePage;
import com.goodedeeds.domain.IResourcePageDao;
import com.youtills.core.logging.Logger;
import com.youtills.action.ActionContext;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rchiluve
 * Date: 2/23/14
 * Time: 7:48 PM
 * To change this template use File | Settings | File Templates.
 */
class ResourcePageDao implements IResourcePageDao{

    private static final Logger LOGGER = Logger.getLogger(ResourcePageDao.class);

    private final SqlSessionFactory sqlSessionFactory;

    private ResourcePageDao() {
        sqlSessionFactory = DomainDao.getInstance().getSqlSessionFactory();
    }

    private static final ResourcePageDao instance = new ResourcePageDao();
    public static ResourcePageDao getInstance(){
        return instance;
    }

    public void saveResourcePage(ActionContext context, IResourcePage resourcePage) {

        if(resourcePage != null){
            if(resourcePage.getPageId() == null){
                createResourcePage(context, resourcePage);
            }else {
                updateResourcePage(context, resourcePage);
            }
        }
    }

    private void updateResourcePage(ActionContext context, IResourcePage resourcePage) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.update("com.goodedeeds.domain.impl.db.resource-page-update", resourcePage);
            session.commit();
        }catch (Exception e){
            LOGGER.error(e);
            session.rollback();
            throw new RuntimeException(e);
        } finally {
            session.close();
        }

    }

    private void createResourcePage(ActionContext context, IResourcePage resourcePage) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.insert("com.goodedeeds.domain.impl.db.resource-page-insert", resourcePage);
            session.commit();
        }catch (Exception e){
            LOGGER.error(e);
            session.rollback();
            throw new RuntimeException(e);
        } finally {
            session.close();
        }

    }

    public ResourcePage getResourcePage(ActionContext context, int parentResourceType, int parentResourceId, int pageType) {

        if(parentResourceType < 1 || parentResourceId < 1 || pageType < 1){
            return null;
        }

        ResourcePage resourcePage = null;
        SqlSession session = sqlSessionFactory.openSession();
        try {
            HashMap<String, Integer> params = new HashMap<String, Integer>(3);
            params.put("parentResourceId", parentResourceId);
            params.put("parentResourceType", parentResourceType);
            params.put("pageType", pageType);
            resourcePage = session.selectOne(
                    "com.goodedeeds.domain.impl.db.resource-page-by-resource", params);
        } finally {
            session.close();
        }

        if(resourcePage == null){
            resourcePage = new ResourcePage();
            resourcePage.setPageResourceId((long) parentResourceId);
            resourcePage.setPageResourceType((long) parentResourceType);
            resourcePage.setPageType(pageType);
        }

        return resourcePage;

    }
    public ResourcePage getResourcePage(ActionContext context, int resourcePageId) {
            SqlSession session = sqlSessionFactory.openSession();
            try {
                ResourcePage resourcePage = session.selectOne(
                        "com.goodedeeds.domain.impl.db.resource-page-by-id", resourcePageId);
                return resourcePage;
            } finally {
                session.close();
            }
    }

    public List<IResourcePage> getAllResourcePages(ActionContext context, int parentResourceType, int parentResourceId) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            HashMap<String, Integer> params = new HashMap<String, Integer>(2);
            params.put("parentResourceType", parentResourceType);
            params.put("parentResourceId", parentResourceId);
            return session.selectList(
                    "com.goodedeeds.domain.impl.db.resource-pages-all", params);
        } finally {
            session.close();
        }
    }
}
